package mylab.starters.mybatis.support.mixedSource.impl;

import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator;
import lombok.extern.slf4j.Slf4j;
import mylab.starters.mybatis.support.mixedSource.TenantDataSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;

import java.util.stream.Collectors;

/**
 * @Description:
 * @author: Sylar
 * @date: 2021年10月29日 3:28 下午
 */
@Slf4j
public class TenantDataSourceServiceImpl implements TenantDataSourceService, ApplicationRunner {

    @Autowired
    private DynamicRoutingDataSource dynamicRoutingDataSource;

    @Autowired
    private DefaultDataSourceCreator creator;

    @Override
    public DynamicRoutingDataSource getDynamicRoutingDs() {
        return dynamicRoutingDataSource;
    }

    @Override
    public DefaultDataSourceCreator getDsCreator() {
        return creator;
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        String names = getAll().keySet().stream().sorted().collect(Collectors.joining(","));
        log.info(CharSequenceUtil.format("成功加载数据源:{}", names));
    }
}
